// Font class
//
// NOTE: This is privated for Text class, user should not call font
//	     functions directly!
//

#ifndef _FONT_H_
#define _FONT_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>          // for FILE
#include "Canvas.h"         // for TCanvas
#include "Image.h"

#define FONT_FX_NORMAL      0	// reset to default, should not combine with any others
#define FONT_FX_OPAQUE		1	// bit0
#define	FONT_FX_BOLD		2	// bit1		
#define FONT_FX_SHADOW		4	// bit2
#define FONT_FX_OUTLINE		8	// bit3
#define FONT_FX_ZOOM_X2		16	// bit4		NOTE: not yet!!!

// ** begin add by Charlie 20060916 ********************************************
typedef struct _FontPageInfo // page info of each page (id)
{
	int serial;  		// serial number of font which cached in this page id
	unsigned int Hit;	// hit counter of this font page id
	int page_id; 		// the page position in pPage where stored the font data
} FontPageInfo;

typedef struct _FontCache
{
	unsigned int	PageNum; 	// font pages can be created
	unsigned int	PageUsed;	// font pages used
	unsigned int	PageSize;	// font page size. if fixed width, 12, it is 18. if varient, it can get by comparing font offset
    char *			filename;	// filename for later access
	FontPageInfo*   pPageInfo;	// points to address (FontPageInfo*) ( FontPageInfo * PageNum)
	char *			pPage;		// points to font data
} FontCache;
// ** end add by Charlie 20060916 **********************************************

/////////////////////////////////////////////////////////////////////////////
// font class

typedef struct _Font
{
/* read only */
    int     Width;          // font size
    int     Height;

    // Image buffer
    TImage* pBitmap;        // buffer for drawing font

    // Fonts pool
    FILE*   pFile;          // where to read the fonts
    FontCache* pFontCache;	// buffer for storing font pages (char*)
    char*   pCache;         // buffer for storing all fonts/or loading one font
    int     TotalNum;       // total number of fonts
    int     PackedSize;     // at least bytes needed for one packed font

	// for speeding fixed width font
    int     OctanSize;      // how many full 8 bits
    int     Exceeding;      // exceeding bits from full 8 bits count
							// MEMO: OctanSize/Exceeding maybe computed in run time -OB
	// for variant width font
    UInt8*  pWidthTbl;
	UInt32* pOffsetTbl;

/* read/write */
    int     FGColor;        // foreground color
    int     BGColor;        // background color
	int		GLColor;		// glow color (shadow also use it this moment)
    int     Effects;        // bit0: is opaque? (transparent?)
							// bit1: is bold?
							// bit2: has shadow?
							// bit3: has outline?
}	TFont;

/* Create a font object */
int  g_CreateFont(TFont *font, const char *filename, int loadToMemory);
int  g_CreateAsciiFont(TFont *font, const char *filename, int loadToMemory);

/* Destroy a font object */
void g_DestroyFont(TFont *font);

/* Get the width of special char */
int  g_GetFontWidth(const TFont *font, int serial);  

#define  g_GetFontWidthMacro(_pfont)    ((_pfont)->Width)

/* Get the height of the font */
int  g_GetFontHeight(const TFont *font);

#define  g_GetFontHeightMacro(_pfont)	((_pfont)->Height)

/* Draw the special char on canvas */
int  g_DrawFont(const TCanvas *canvas, const TFont *font, int x, int y, int serial);
int g_DrawAsciiFont(const TCanvas *canvas, const TFont *font, int x, int y, int serial);

/* Draw the special char on canvas w/ rotate */
int  g_DrawFontR(const TCanvas *canvas, const TFont *font, int x, int y, int serial, int rotate);
int g_DrawAsciiFontR(const TCanvas *canvas, const TFont *font, int x, int y, int serial, int rotate);

#ifdef __cplusplus
}
#endif

#endif  // _FONT_H_
